package com.shaw.workrecord.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaw.workrecord.dto.Room;
import com.shaw.workrecord.dto.RoomMember;
import com.shaw.workrecord.entity.PageRequest;
import com.shaw.workrecord.entity.PageResponse;
import com.shaw.workrecord.enums.RoomMemberType;
import com.shaw.workrecord.mapper.RoomMapper;
import com.shaw.workrecord.mapper.RoomMemberMapper;
import com.shaw.workrecord.pojo.game.request.RoomAddRequest;
import com.shaw.workrecord.pojo.game.request.RoomEditRequest;
import com.shaw.workrecord.pojo.game.response.RoomResponse;
import com.shaw.workrecord.service.IRoomService;
import com.shaw.workrecord.utils.JWTUtils;
import com.shaw.workrecord.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author jiangjunhui
 * @description
 * @date 2022/9/15 10:39
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {

    @Autowired
    private RoomMemberMapper roomMemberMapper;

    /**
     * 查询分页
     *
     * @param pageRequest 分页请求
     * @return
     */
    @Override
    public PageResponse<RoomResponse> getRoomPage(PageRequest pageRequest) {
        LambdaQueryWrapper<Room> wrapper = new LambdaQueryWrapper<>();
        Page<Room> page = new Page<>(pageRequest.getPageIndex(), pageRequest.getPageSize());
        Page<Room> result = baseMapper.selectPage(page, wrapper);
        return PageUtils.toPageResponse(result, RoomResponse.class);
    }

    /**
     * 新增房间
     *
     * @param request 请求对象
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addRoom(RoomAddRequest request) {
        Room room = BeanUtil.copyProperties(request, Room.class);
        int result = baseMapper.insert(room);
        Long userId = JWTUtils.getUserId();
        RoomMember roomMember = new RoomMember();
        roomMember.setMemberId(userId);
        roomMember.setType(RoomMemberType.Owner.getType());
        roomMember.setRoomId(room.getId());
        roomMember.setSeat(1);
        int r = roomMemberMapper.insert(roomMember);
        return result > 0 && r > 0;
    }

    /**
     * 编辑房间
     *
     * @param request 请求对象
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editRoom(RoomEditRequest request) {
        Room room = BeanUtil.copyProperties(request, Room.class);
        int result = baseMapper.updateById(room);
        return result > 0;
    }

    /**
     * 删除房间
     *
     * @param id 房间id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteRoom(Long id) {
        int result = baseMapper.deleteById(id);
        return result > 0;
    }
}
